#include <iostream>
//#include <mysql_connection.h>
#include "mysql++.h"
 
using namespace std;
 
int main()
{
    const char* db = "oj_problems";
    const char* server = "localhost";
    const char* user = "ojuser";
    const char* password = "123456";
 
    // 创建数据库连接
    mysqlpp::Connection conn(false);
    // 连接数据库
    if (conn.connect(db, server, user, password))
    {
        cout << "connect db succeed." << endl;
        // 查询操作，查询结果保存在 query 中
        mysqlpp::Query query = conn.query("SELECT * FROM Problem");
        // 处理并打印查询结果
        if (mysqlpp::StoreQueryResult res = query.store())
        {
            // 输出数据左对齐
            cout.setf(ios::left);
            // 字段宽度为20位
            // 打印表的字段名
            cout << setw(21) << "id" <<
                    setw(21) << "name" <<
                    setw(21) << "difficulty" <<
                    endl;
            // 打印查询结果
            mysqlpp::StoreQueryResult::const_iterator it;
            for (it = res.begin(); it != res.end(); ++it)
            {
                mysqlpp::Row row = *it;
                cout << setw(20) << row[0] << ' ' <<
                        setw(20) << row[1] << ' ' <<
                        setw(20) << row[2] << ' ' <<
                        endl;
            }
        }
    }
    else
    {
        cout << "connect db fail." << endl;
    }
    return 0;
}

